package com.shadow.baiqun.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.rey.material.app.Dialog;
import com.rey.material.widget.Spinner;
import com.shadow.baiqun.R;
import com.shadow.baiqun.config.ItemField;
import com.shadow.baiqun.global.GlobalDataManager;
import com.shadow.baiqun.global.LocationEntity;
import com.shadow.ui.recycler.MultipleItemEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * on 2018/5/29
 */
public class LocationConfirmDialog extends Dialog {
    private OnActionListener mListener;
    private String mCurrentDepId;
    private String mCurrentDepName;
    private int mCurrentStationPosition = 0;

    public LocationConfirmDialog(Context context, String partName, String partCode, String location) {
        super(context);
        init(context, partName, partCode, location);
    }

    private void init(Context context, String partName, final String partCode, String locationId) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_location, null);
        setContentView(view);

        TextView etPartName = view.findViewById(R.id.et_part_name);
        etPartName.setText(partName);
        final EditText etPartCount = view.findViewById(R.id.et_part_code);
        etPartCount.setText(partCode);
        com.rey.material.widget.Spinner spinnerLocation = view.findViewById(R.id.spinner_state);

        List<String> items = new ArrayList<>();
        final List<MultipleItemEntity> locationList = GlobalDataManager.getInstance().getLocationEntityList();
        if (locationList != null && !locationList.isEmpty()) {
            for (int i = 0, size = locationList.size(); i < size; i++) {
                MultipleItemEntity multipleItemEntity = locationList.get(i);
                LocationEntity entity = multipleItemEntity.getField(ItemField.ITEM_OBJ);
                if (i == 0) {
                    //默认第一个
                    mCurrentStationPosition = 0;
                    mCurrentDepId = entity.getDepotId();
                    mCurrentDepName = entity.getDepotName();
                }
                items.add(entity.getDepotName());
                if (entity.getDepotId().equals(locationId)) {
                    mCurrentDepId = locationId;
                    mCurrentDepName = entity.getDepotName();
                    mCurrentStationPosition = i;
                } else {
                    if (i == 0) {
                        mCurrentDepId = entity.getDepotId();
                        mCurrentDepName = entity.getDepotName();
                    }
                }
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.row_spn, items);
            adapter.setDropDownViewResource(R.layout.row_spn_dropdown);
            spinnerLocation.setAdapter(adapter);
            spinnerLocation.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
                @Override
                public void onItemSelected(Spinner parent, View view, int position, long id) {
                    MultipleItemEntity multipleItemEntity = locationList.get(position);
                    LocationEntity entity = multipleItemEntity.getField(ItemField.ITEM_OBJ);
                    mCurrentDepId = entity.getDepotId();
                    mCurrentDepName = entity.getDepotName();
                }
            });
            spinnerLocation.setSelection(mCurrentStationPosition);
        }
        view.findViewById(R.id.tv_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    String partCount = etPartCount.getText().toString();
                    int count = partCount.isEmpty() ? 0 : Integer.parseInt(partCount);
                    mListener.onCommit(mCurrentDepId, mCurrentDepName, count);
                    dismiss();
                }
            }
        });
    }

    public void setOnActionListener(OnActionListener listener) {
        mListener = listener;
    }

    public interface OnActionListener {
        public void onCommit(String currentDepId, String depId, int number);
    }
}
